import json

from django.core import serializers
from rest_framework.response import Response
from rest_framework.views import APIView

from hospital_model.models import AgreementModel, DepartmentModel, ProjectModel, TimeModel, DataTimeModel, \
    TimeQuantumModel, ReclassifyModel
from hospital_model.serializer import DepartmentViewSerializer
from reservation_model.models import StatisticsModel, ReservationOrderModel
from user_model.models import ComplainModel
from datetime import datetime
from utils.check import is_check, is_sex, verify_datetime, verify_week, is_verify, is_timing, GetInformation, getrand


# 首页 导航栏
class HomeView(APIView):
    def get(self, *args, **kwargs):
        list_data = []
        department = DepartmentModel.objects.filter(d_status=0)
        agreements = (json.loads(serializers.serialize('json', department)))
        for agreement in agreements:
            dict_data = {}
            dict_data['project_name'] = agreement['fields']['d_name']
            dict_data['id'] = agreement['pk']
            dict_data['image'] = 'http://192.168.3.113:8013/static/project/01.jpg'
            # agreement_json = ProjectModel.objects.filter(p_key=agreement['pk']).all()
            # if agreement_json.count() == 0:
            #     dict_data['project'] = []
            #     list_data.append(dict_data)
            # else:
            #     project_list = []
            #     agreement_list = (json.loads(serializers.serialize('json', agreement_json)))
            #     for agreement_li in agreement_list:
            #         json_data = {}
            #         project_id = agreement_li['pk']
            #         # print(agreement_li['fields'])
            #         project_name = agreement_li['fields']['p_name']
            #         p_money = agreement_li['fields']['p_money']
            #         p_note = agreement_li['fields']['p_note']
            #         start_age = agreement_li['fields']['start_age']
            #         end_age = agreement_li['fields']['end_age']
            #         c_sex = agreement_li['fields']['c_sex']
            #         content = agreement_li['fields']['content']
            #         image = 'http:192.168.3.113:8000/' + agreement_li['fields']['image']
            #         json_data['project_id'] = project_id
            #         json_data['project_name'] = project_name
            #         json_data['p_money'] = p_money
            #         json_data['p_note'] = p_note
            #
            #         json_data['start_age'] = start_age
            #         json_data['end_age'] = end_age
            #         json_data['c_sex'] = c_sex
            #         json_data['image'] = image
            #         json_data['content'] = content
            #         project_list.append(json_data)
            #     dict_data['project'] = project_list
            #     list_data.append(dict_data)
            list_data.append(dict_data)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': list_data
        }
        return Response(data)


# 预防接种
class YFView(APIView):
    def post(self, request, *args, **kwargs):
        id = request.POST['id']
        data_list = []
        two_list = json.loads(serializers.serialize('json', ReclassifyModel.objects.filter(d_key__d_id=id)))
        for i in two_list:
            data_dict = {}
            data_dict['id'] = i['pk']
            data_dict['d_name'] = i['fields']['d_name']
            data_dict['d_type'] = i['fields']['d_type']
            data_dict['image'] = 'http://192.168.3.113:8013/static/project/01.jpg'
            data_list.append(data_dict)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': data_list
        }
        return Response(data)


# 母婴保健
class MoMInfantView(APIView):
    def post(self, request, *args, **kwargs):
        id = request.POST['id']
        data_list = []
        two_list = json.loads(serializers.serialize('json', ReclassifyModel.objects.filter(d_key__d_id=id)))
        for i in two_list:
            data_dict = {}
            data_dict['id'] = i['pk']
            data_dict['d_name'] = i['fields']['d_name']
            data_dict['d_type'] = i['fields']['d_type']
            data_dict['image'] = 'http://192.168.3.113:8013/static/project/01.jpg'
            data_list.append(data_dict)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': data_list
        }
        return Response(data)


# 老年保健
class OldHealthtView(APIView):
    def post(self, request, *args, **kwargs):
        id = request.POST['id']
        data_list = []
        two_list = json.loads(serializers.serialize('json', ReclassifyModel.objects.filter(d_key__d_id=id)))
        for i in two_list:
            data_dict = {}
            data_dict['id'] = i['pk']
            data_dict['d_name'] = i['fields']['d_name']
            data_dict['d_type'] = i['fields']['d_type']
            data_dict['image'] = 'http://192.168.3.113:8013/static/project/01.jpg'
            data_list.append(data_dict)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': data_list
        }
        return Response(data)


# 99体检、老年体检、妇女体检
class OtherDetailsView(APIView):
    def post(self, request, *args, **kwargs):
        id = request.POST['id']
        tijian = json.loads(serializers.serialize('json', ProjectModel.objects.filter(p_key__d_id=id)))[0]
        data_dict = {}
        # 项目详情 字段获取
        data_dict['id'] = tijian['pk']
        data_dict['p_name'] = tijian['fields']['p_name']
        data_dict['p_money'] = tijian['fields']['p_money']
        data_dict['start_age'] = tijian['fields']['start_age']
        data_dict['end_age'] = tijian['fields']['end_age']
        data_dict['p_note'] = tijian['fields']['p_note']
        data_dict['content'] = tijian['fields']['content']
        # 项目预约时间点
        for i in tijian['fields']['p_key_time']:
            datas = json.loads(serializers.serialize('json', TimeQuantumModel.objects.filter(t_id=i)))[0]
            start_time = datas['fields']['start_time']
            end_time = datas['fields']['end_time']
            data_dict['start_time'] = start_time
            data_dict['end_time'] = end_time
            week = []
            for i in datas['fields']['d_key']:
                aa = json.loads(serializers.serialize('json', DataTimeModel.objects.filter(d_id=i)))[0]['fields'][
                    'd_data']
                week.append(aa)
            data_dict['week'] = week
            timing_list = []
            for i in datas['fields']['d_time']:
                bb = json.loads(serializers.serialize('json', TimeModel.objects.filter(t_id=i)))[0]['fields']
                timing_list.append(bb)
            data_dict['timing_list'] = timing_list
        data = {
            'msg': '查询成功',
            'code': 2000,
            'data': data_dict
        }
        return Response(data)


# 预防接种、母婴保健、老年保健二级分类展示
class BaoView(APIView):
    def post(self, request, *args, **kwargs):
        id = request.POST['id']
        id_data_list = json.loads(serializers.serialize('json', ProjectModel.objects.filter(p_key__d_id=id)))
        data_list = []
        for i in id_data_list:
            data_dict = {}
            data_dict['id'] = i['pk']
            data_dict['name'] = i['fields']['p_name']
            data_dict['image'] = 'http://192.168.3.113:8013/static/project/01.jpg'
            data_list.append(data_dict)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': data_list
        }
        return Response(data)


# 预防接种、母婴保健、老年保健二级分类进入详情页面
class BaoTwoView(APIView):
    def post(self, request, *args, **kwargs):
        id = request.POST['id']
        tijian = json.loads(serializers.serialize('json', ProjectModel.objects.filter(p_id=id)))[0]
        data_dict = {}
        # 项目详情 字段获取
        data_dict['id'] = tijian['pk']
        data_dict['p_name'] = tijian['fields']['p_name']
        data_dict['p_money'] = tijian['fields']['p_money']
        data_dict['start_age'] = tijian['fields']['start_age']
        data_dict['end_age'] = tijian['fields']['end_age']
        data_dict['p_note'] = tijian['fields']['p_note']
        data_dict['content'] = tijian['fields']['content']
        # 项目预约时间点http://127.0.0.1:8000/api/hospital/detialstwo
        for i in tijian['fields']['p_key_time']:
            datas = json.loads(serializers.serialize('json', TimeQuantumModel.objects.filter(t_id=i)))[0]
            start_time = datas['fields']['start_time']
            end_time = datas['fields']['end_time']
            data_dict['start_time'] = start_time
            data_dict['end_time'] = end_time
            week = []
            for i in datas['fields']['d_key']:
                aa = json.loads(serializers.serialize('json', DataTimeModel.objects.filter(d_id=i)))[0]['fields'][
                    'd_data']
                week.append(aa)
            data_dict['week'] = week
            timing_list = []
            for i in datas['fields']['d_time']:
                bb = json.loads(serializers.serialize('json', TimeModel.objects.filter(t_id=i)))[0]['fields']
                timing_list.append(bb)
            data_dict['timing_list'] = timing_list
        data = {
            'msg': '查询成功',
            'code': 2000,
            'data': data_dict
        }
        return Response(data)


#
class OthersDetailsView(APIView):
    def post(self, request, *args, **kwargs):
        id = request.POST['id']
        # print(json.loads(serializers.serialize('json',DepartmentModel.objects.filter(reclassify__d_id=id)))[0]['pk'])
        tijian = json.loads(serializers.serialize('json', ProjectModel.objects.filter(p_key__reclassify__d_id=id)))[0]
        data_dict = {}
        # 项目详情 字段获取
        data_dict['id'] = tijian['pk']
        data_dict['p_name'] = tijian['fields']['p_name']
        data_dict['p_money'] = tijian['fields']['p_money']
        data_dict['start_age'] = tijian['fields']['start_age']
        data_dict['end_age'] = tijian['fields']['end_age']
        data_dict['p_note'] = tijian['fields']['p_note']
        data_dict['content'] = tijian['fields']['content']
        # 项目预约时间点
        for i in tijian['fields']['p_key_time']:
            datas = json.loads(serializers.serialize('json', TimeQuantumModel.objects.filter(t_id=i)))[0]
            start_time = datas['fields']['start_time']
            end_time = datas['fields']['end_time']
            data_dict['start_time'] = start_time
            data_dict['end_time'] = end_time
            week = []
            for i in datas['fields']['d_key']:
                aa = json.loads(serializers.serialize('json', DataTimeModel.objects.filter(d_id=i)))[0]['fields'][
                    'd_data']
                week.append(aa)
            data_dict['week'] = week
            timing_list = []
            for i in datas['fields']['d_time']:
                bb = json.loads(serializers.serialize('json', TimeModel.objects.filter(t_id=i)))[0]['fields']
                timing_list.append(bb)
            data_dict['timing_list'] = timing_list
        #
        data = {
            'msg': '查询成功',
            'code': 2000,
            'data': data_dict
        }
        return Response(data)


# 体检预约查询
class OldMedicalView(APIView):
    def post(self, request, *args, **kwargs):
        # 体检项目id
        tj_id = request.POST['t_id']
        agreement_json = TimeQuantumModel.objects.filter(project__p_id=tj_id).all()
        agreement_lists = (json.loads(serializers.serialize('json', agreement_json)))
        p_json = ProjectModel.objects.filter(p_id=tj_id)
        p = (json.loads(serializers.serialize('json', p_json)))[0]
        time_quantum_list = []
        for agreement_list in agreement_lists:
            ProjectModel.objects.filter()
            time_quantum_dict = {}
            point_in_time = []
            time_quantum_dict['pk'] = tj_id
            time_quantum_dict['p_name'] = p['fields']['p_name']
            time_quantum_dict['p_money'] = p['fields']['p_money']
            time_quantum_dict['p_prepay'] = p['fields']['p_prepay']
            time_quantum_dict['p_note'] = p['fields']['p_note']
            time_quantum_dict['p_day'] = p['fields']['p_day']
            time_quantum_dict['p_number'] = p['fields']['p_number']
            time_quantum_dict['t_name'] = agreement_list['fields']['t_name']
            time_quantum_dict['start_time'] = agreement_list['fields']['start_time']
            time_quantum_dict['end_time'] = agreement_list['fields']['end_time']
            data_jsons = DataTimeModel.objects.filter(time__t_id=agreement_list['pk']).all()
            serialization_list = (json.loads(serializers.serialize('json', data_jsons)))
            week_list = []
            for i in serialization_list:
                week_list.append(i['fields']['d_data'])
            time_quantum_dict['week'] = week_list
            for a in agreement_list['fields']['d_time']:
                data_jsons = TimeModel.objects.filter(t_id=a)
                point_in_time_dict = {}
                serialization_li = (json.loads(serializers.serialize('json', data_jsons)))
                for b in serialization_li:
                    # 时间点
                    point_in_time_dict['time_id'] = b['pk']
                    point_in_time_dict['t_name'] = b['fields']['t_name']
                    point_in_time_dict['t_number'] = b['fields']['t_number']
                    point_in_time_dict['t_start_time'] = b['fields']['t_start_time']
                    point_in_time_dict['t_end_time'] = b['fields']['t_end_time']
                point_in_time.append(point_in_time_dict)
            time_quantum_dict['datetime'] = point_in_time
            time_quantum_list.append(time_quantum_dict)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': time_quantum_list
        }
        return Response(data)


# 预约
class OrderView(APIView):
    def post(self, request, *args, **kwargs):
        # 体检项目id
        project_id = request.POST['project_id']  # 预约项目id
        # 预约人身份证号
        ID_card = request.POST['ID_card']  # 预约人身份证号
        # 预约时间
        time = request.POST['time']  # 预约时间
        # 预约时间点
        timinga = request.POST['timing'][0:5]  # 预约时间点
        is_project = ProjectModel.objects.filter(p_id=project_id)
        # 判断项目是否存在

        if len(is_project) == 0:

            data = {
                'msg': "查询没有此项目",
                'code': 4000,
            }
            return Response(data)
        else:
            # 将预约项目的基本信息放在一个字典中
            project_details = {}
            project_json = json.loads(serializers.serialize('json', is_project))[0]
            agreement_json = TimeQuantumModel.objects.filter(project__p_id=project_id).all()
            agreement_lists = (json.loads(serializers.serialize('json', agreement_json)))[0]
            # 星期几id
            d_keys = agreement_lists['fields']['d_key']
            week = []
            timing = []
            for i in d_keys:
                weeks = json.loads(serializers.serialize('json', DataTimeModel.objects.filter(d_id=i)))[0]
                week.append(weeks['fields']['d_data'])
            # 时间点id
            d_times = agreement_lists['fields']['d_time']
            for i in d_times:
                timings = json.loads(serializers.serialize('json', TimeModel.objects.filter(t_id=i)))[0]
                timing.append(timings['fields'])
            # 预约项目id
            id = project_json['pk']
            # 项目名称
            name = project_json['fields']['p_name']
            # 体检费
            money = project_json['fields']['p_money']
            # 提前多少天预约
            day = project_json['fields']['p_day']
            # 免费次数
            free = project_json['fields']['free_number']
            # 提前多少小时取消预约
            cancel_time = project_json['fields']['cancel_time']
            # 开始年龄
            start_age = project_json['fields']['start_age']
            # 结束年龄
            end_age = project_json['fields']['end_age']
            # 性别
            c_sex = project_json['fields']['c_sex']
            ############                 时间                    #############
            # 开始时间
            start_time = agreement_lists['fields']['start_time']
            # 结束时间
            end_time = agreement_lists['fields']['end_time']
            project_details['id'] = id
            project_details['name'] = name
            project_details['money'] = money
            project_details['day'] = day
            project_details['free'] = free
            project_details['cancel_time'] = cancel_time
            project_details['start_age'] = start_age
            project_details['end_age'] = end_age
            project_details['c_sex'] = c_sex
            project_details['start_time'] = start_time
            project_details['end_time'] = end_time
            project_details['week'] = week
            project_details['timing'] = timing
            """
           判断姓名
           0：全部可以预约
           1：男性可以预约
           2：女性可以预约
           ['09:30', '08:00', '10:00', '09:00', '07:30', '08:30']
            """
            # print(c_sex, time[0:4])

            if int(c_sex) == 0:
                # 判断年龄是否符合  and 预约日期是否符合  and 星期几是否符合规范 and 时间点是否在范围内  and 是否超过预约天数
                print(is_check(start_age, end_age, GetInformation(ID_card).get_age()),
                      verify_datetime(start_time, end_time, time),
                      verify_week(time) in week,
                      timinga in [i['t_start_time'][0:5] for i in timing],
                      (is_timing(time) > 0 and is_timing(time) < int(day)),
                      time, day
                      )
                if is_check(start_age, end_age, GetInformation(ID_card).get_age()) \
                        and verify_datetime(start_time, end_time, time) \
                        and verify_week(time) in week \
                        and timinga in [i['t_start_time'][0:5] for i in timing] \
                        and (is_timing(time) > 0 and is_timing(time) < int(day)):
                    """
                    全部可以预约  不需要再进行性别校验
                    判断年龄范围是否符合
                    获取年龄 GetInformation(ID_card).get_age()  
                    """
                    # ReservationOrderModel.objects.create(r_IDcard=ID_card, r_order_number=1,project_id=project_id, r_time=timinga,
                    #                                      r_data=time)
                    yuyue_number = ReservationOrderModel.objects.filter(r_IDcard=ID_card, r_data__contains=time[0:5])
                    # 判断是否预约过
                    if len(yuyue_number) == 0:
                        num_list = StatisticsModel.objects.filter(project_id=project_id, date_id=time, time=timinga)
                        # 还没有人预约过
                        if len(num_list) == 0:
                            ReservationOrderModel.objects.create(r_order_number=getrand(), r_IDcard=ID_card,
                                                                 project_id=project_id, r_time=timinga, r_data=time)
                            StatisticsModel.objects.create(project_id=project_id, date_id=time, time=timinga, number=1)
                            data = {
                                'msg': "预约成功",
                                'code': 2000,
                            }
                            return Response(data)
                        else:
                            numbers = json.loads(serializers.serialize('json', num_list))[0]['fields']['number']
                            if int(numbers) < 5:
                                ReservationOrderModel.objects.create(r_order_number=getrand(), r_IDcard=ID_card,
                                                                     project_id=project_id, r_time=timinga, r_data=time)
                                StatisticsModel.objects.filter(project_id=project_id, date_id=time,
                                                               time=timinga).update(number=int(numbers) + 1)
                                data = {
                                    'msg': "预约成功",
                                    'code': 2000,
                                }
                                return Response(data)
                            else:
                                data = {
                                    'msg': "该时间点预约已满",
                                    'code': 4000,
                                }
                                return Response(data)
                    else:
                        if len(yuyue_number) < int(free):
                            pass
                        data = {
                            'msg': "预约过",
                            'code': 2000,
                        }
                        return Response(data)

                else:
                    data = {
                        'msg': "不在预约范围",
                        'code': 4000,
                    }
                    return Response(data)

            elif int(c_sex) == 1:
                """
                只有男性可以预约  通过身份证校验性别
                """
                data = {
                    'msg': "男",
                    'code': 2000,
                }
                return Response(data)
            else:
                """
                只有女性可以预约  通过身份证校验性别
                """
                data = {
                    'msg': "女",
                    'code': 2000,
                }
                return Response(data)
            # is_statistics = StatisticsModel.objects.filter(s_id=project_id)
            # # 判断统计表中是否存在
            # if is_statistics == 0:
            #     data = {
            #         'msg': "查询有此项目",
            #         'code': 2000,
            #     }
            #     return Response(data)
            # else:
            #     data = {
            #         'msg': "查询有此项目",
            #         'code': 2000,
            #     }
            #     return Response(data)


# # 母婴保健
# 查询投诉建议
# class SuggestionView(APIView):
#     def get(self, *args, **kwargs):
#         data = []
#         time = str(datetime.now().strftime('%Y-%m-%d'))
#         print(type(time), time)
#         all_complain = ComplainModel.objects.filter(c_time__contains=time)
#         data_lists = (json.loads(serializers.serialize('json', all_complain)))
#         for data_list in data_lists:
#             data.append(data_list['fields'])
#         data = {
#             'msg': "查询当天投诉建议",
#             'code': 2000,
#             'data': data
#         }
#         return Response(data)
#
# # 协议表
# class AgreementView(APIView):
#     def get(self, *args, **kwargs):
#         agreement = AgreementModel.objects.filter(a_status='0').all()
#         agreements = (json.loads(serializers.serialize('json', agreement)))[0]
#         data = agreements['fields']
#         data = {
#             'msg': "查询当天投诉建议",
#             'code': 2000,
#             'data': data
#         }
#         return Response(data)
#
#     def post(self, request, *args, **kwargs):
#         content = request.POST['content']
#         AgreementModel.objects.create(a_content=content)
#         data = {
#             'msg': "添加成功",
#             'code': 2000,
#         }
#         return Response(data)

# class MaternalInfantView(APIView):
#     def get(self, *args, **kwargs):
#         list_data = []
#         department = DepartmentModel.objects.filter(department__p_type=2)
#         agreements = (json.loads(serializers.serialize('json', department)))
#         for agreement in agreements:
#             dict_data = {}
#             dict_data['project_name'] = agreement['fields']['d_name']
#             agreement_json = ProjectModel.objects.filter(p_key=agreement['pk']).all()
#             if agreement_json.count() == 0:
#                 dict_data['project'] = []
#                 list_data.append(dict_data)
#             else:
#                 project_list = []
#                 agreement_list = (json.loads(serializers.serialize('json', agreement_json)))
#                 for agreement_li in agreement_list:
#                     json_data = {}
#                     project_id = agreement_li['pk']
#                     # print(agreement_li['fields'])
#                     project_name = agreement_li['fields']['p_name']
#                     p_money = agreement_li['fields']['p_money']
#                     p_note = agreement_li['fields']['p_note']
#                     start_age = agreement_li['fields']['start_age']
#                     end_age = agreement_li['fields']['end_age']
#                     c_sex = agreement_li['fields']['c_sex']
#                     content = agreement_li['fields']['content']
#                     image = 'http:192.168.3.113:8000/' + agreement_li['fields']['image']
#                     json_data['project_id'] = project_id
#                     json_data['project_name'] = project_name
#                     json_data['p_money'] = p_money
#                     json_data['p_note'] = p_note
#
#                     json_data['start_age'] = start_age
#                     json_data['end_age'] = end_age
#                     json_data['c_sex'] = c_sex
#                     json_data['image'] = image
#                     json_data['content'] = content
#                     project_list.append(json_data)
#                 dict_data['project'] = project_list
#                 list_data.append(dict_data)
#
#         data = {
#             'msg': "查询成功",
#             'code': 2000,
#             'data': list_data
#         }
#         return Response(data)
# # 老年保健
# class SeniumView(APIView):
#     def get(self, *args, **kwargs):
#         list_data = []
#         department = DepartmentModel.objects.filter(department__p_type=3)
#         agreements = (json.loads(serializers.serialize('json', department)))
#         for agreement in agreements:
#             dict_data = {}
#             dict_data['project_name'] = agreement['fields']['d_name']
#             agreement_json = ProjectModel.objects.filter(p_key=agreement['pk']).all()
#             if agreement_json.count() == 0:
#                 dict_data['project'] = []
#                 list_data.append(dict_data)
#             else:
#                 project_list = []
#                 agreement_list = (json.loads(serializers.serialize('json', agreement_json)))
#                 for agreement_li in agreement_list:
#                     json_data = {}
#                     project_id = agreement_li['pk']
#                     # print(agreement_li['fields'])
#                     project_name = agreement_li['fields']['p_name']
#                     p_money = agreement_li['fields']['p_money']
#                     p_note = agreement_li['fields']['p_note']
#                     start_age = agreement_li['fields']['start_age']
#                     end_age = agreement_li['fields']['end_age']
#                     c_sex = agreement_li['fields']['c_sex']
#                     content = agreement_li['fields']['content']
#                     image = 'http:192.168.3.113:8000/' + agreement_li['fields']['image']
#                     json_data['project_id'] = project_id
#                     json_data['project_name'] = project_name
#                     json_data['p_money'] = p_money
#                     json_data['p_note'] = p_note
#
#                     json_data['start_age'] = start_age
#                     json_data['end_age'] = end_age
#                     json_data['c_sex'] = c_sex
#                     json_data['image'] = image
#                     json_data['content'] = content
#                     project_list.append(json_data)
#                 dict_data['project'] = project_list
#                 list_data.append(dict_data)
#
#         data = {
#             'msg': "查询成功",
#             'code': 2000,
#             'data': list_data
#         }
#         return Response(data)
# 老年体检、99体检、妇女体检
# class OtherView(APIView):
#     def get(self, *args, **kwargs):
#         list_data = []
#         department = DepartmentModel.objects.filter(department__p_type=0)
#         agreements = (json.loads(serializers.serialize('json', department)))
#         for agreement in agreements:
#             dict_data = {}
#             dict_data['project_name'] = agreement['fields']['d_name']
#             agreement_json = ProjectModel.objects.filter(p_key=agreement['pk']).all()
#             if agreement_json.count() == 0:
#                 dict_data['project'] = []
#                 list_data.append(dict_data)
#             else:
#                 project_list = []
#                 agreement_list = (json.loads(serializers.serialize('json', agreement_json)))
#                 for agreement_li in agreement_list:
#                     json_data = {}
#                     project_id = agreement_li['pk']
#                     # print(agreement_li['fields'])
#                     project_name = agreement_li['fields']['p_name']
#                     p_money = agreement_li['fields']['p_money']
#                     p_note = agreement_li['fields']['p_note']
#                     start_age = agreement_li['fields']['start_age']
#                     end_age = agreement_li['fields']['end_age']
#                     c_sex = agreement_li['fields']['c_sex']
#                     content = agreement_li['fields']['content']
#                     image = 'http:192.168.3.113:8000/' + agreement_li['fields']['image']
#                     json_data['project_id'] = project_id
#                     json_data['project_name'] = project_name
#                     json_data['p_money'] = p_money
#                     json_data['p_note'] = p_note
#
#                     json_data['start_age'] = start_age
#                     json_data['end_age'] = end_age
#                     json_data['c_sex'] = c_sex
#                     json_data['image'] = image
#                     json_data['content'] = content
#                     project_list.append(json_data)
#                 dict_data['project'] = project_list
#                 list_data.append(dict_data)
#
#         data = {
#             'msg': "查询成功",
#             'code': 2000,
#             'data': list_data
#         }
#         return Response(data)
# 预防接种项目
# class DepartmentView(APIView):
#     def get(self, *args, **kwargs):
#         list_data = []
#         department = DepartmentModel.objects.filter(department__p_type=1)
#         agreements = (json.loads(serializers.serialize('json', department)))
#         for agreement in agreements:
#             dict_data = {}
#             dict_data['project_name'] = agreement['fields']['d_name']
#             agreement_json = ProjectModel.objects.filter(p_key=agreement['pk']).all()
#             if agreement_json.count() == 0:
#                 dict_data['project'] = []
#                 list_data.append(dict_data)
#             else:
#                 project_list = []
#                 agreement_list = (json.loads(serializers.serialize('json', agreement_json)))
#                 for agreement_li in agreement_list:
#                     json_data = {}
#                     project_id = agreement_li['pk']
#                     # print(agreement_li['fields'])
#                     project_name = agreement_li['fields']['p_name']
#                     p_money = agreement_li['fields']['p_money']
#                     p_note = agreement_li['fields']['p_note']
#                     start_age = agreement_li['fields']['start_age']
#                     end_age = agreement_li['fields']['end_age']
#                     c_sex = agreement_li['fields']['c_sex']
#                     content = agreement_li['fields']['content']
#                     image = 'http:192.168.3.113:8000/' + agreement_li['fields']['image']
#                     json_data['project_id'] = project_id
#                     json_data['project_name'] = project_name
#                     json_data['p_money'] = p_money
#                     json_data['p_note'] = p_note
#
#                     json_data['start_age'] = start_age
#                     json_data['end_age'] = end_age
#                     json_data['c_sex'] = c_sex
#                     json_data['image'] = image
#                     json_data['content'] = content
#                     project_list.append(json_data)
#                 dict_data['project'] = project_list
#                 list_data.append(dict_data)
#
#         data = {
#             'msg': "查询成功",
#             'code': 2000,
#             'data': list_data
#         }
#         return Response(data)


"""
# 体检预约查询
class OldMedicalView(APIView):
    def post(self, request, *args, **kwargs):
        # 体检项目id
        tj_id = request.POST['t_id']
        agreement_json = TimeQuantumModel.objects.filter(project__p_id=tj_id).all()
        agreement_lists = (json.loads(serializers.serialize('json', agreement_json)))
        # print(type(ProjectModel.objects.filter(p_id=tj_id).first()))
        p_json = ProjectModel.objects.filter(p_id=tj_id)
        p = (json.loads(serializers.serialize('json', p_json)))[0]
        time_quantum_list = []
        for agreement_list in agreement_lists:
            ProjectModel.objects.filter()
            time_quantum_dict = {}
            point_in_time = []
            time_quantum_dict['pk'] = agreement_list['pk']
            time_quantum_dict['p_name'] = p['fields']['p_name']
            time_quantum_dict['p_money'] = p['fields']['p_money']
            time_quantum_dict['p_prepay'] = p['fields']['p_prepay']
            time_quantum_dict['p_note'] = p['fields']['p_note']
            time_quantum_dict['p_day'] = p['fields']['p_day']
            time_quantum_dict['p_number'] = p['fields']['p_number']
            time_quantum_dict['t_name'] = agreement_list['fields']['t_name']
            time_quantum_dict['start_time'] = agreement_list['fields']['start_time']
            time_quantum_dict['end_time'] = agreement_list['fields']['end_time']
            data_jsons = DataTimeModel.objects.filter(time__t_id=agreement_list['pk']).all()
            serialization_list = (json.loads(serializers.serialize('json', data_jsons)))
            week_list = []
            for i in serialization_list:
                week_list.append(i['fields']['d_data'])
            time_quantum_dict['week'] = week_list
            for a in agreement_list['fields']['d_time']:
                data_jsons = TimeModel.objects.filter(t_id=a)
                point_in_time_dict = {}
                serialization_li = (json.loads(serializers.serialize('json', data_jsons)))
                for b in serialization_li:
                    # 时间点
                    point_in_time_dict['t_name'] = b['fields']['t_name']
                    point_in_time_dict['t_number'] = b['fields']['t_number']
                    point_in_time_dict['t_start_time'] = b['fields']['t_start_time']
                    point_in_time_dict['t_end_time'] = b['fields']['t_end_time']
                point_in_time.append(point_in_time_dict)
            time_quantum_dict['datetime'] = point_in_time
            time_quantum_list.append(time_quantum_dict)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': time_quantum_list
        }
        return Response(data)
"""
